package com.house.controller;

import com.house.entity.*;
import com.house.mapper.HouseImageMapper;
import com.house.service.*;
import com.house.vo.JsonVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/house")
public class HouseController {

    @Autowired
    private AreaService areaService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private UserService userService;
    @Autowired
    private NewsService newsService;
    @Autowired
    private AccusationService accusationService;
    @Autowired
    private CollectService collectService;
    @Autowired
    private HouseImageMapper houseImageMapper;
    @RequestMapping(value = "/inf/{id}")
    public ModelAndView houseInfPage(@PathVariable String id,HttpSession session){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("house/houseInf");
        House house=houseService.getById(Integer.parseInt(id));
        List<String> tags= Arrays.asList(house.getTag().split(","));
        List<HouseImage> houseImageList=houseService.findByHouseId(house.getId());
        UserLogin userLogin2=userService.findByid(house.getUserId());
        UserInf userInf=userService.findByUserLoginId(userLogin2.getId());
        modelAndView.addObject("house",house);
        modelAndView.addObject("tags",tags);
        modelAndView.addObject("imageList",houseImageList);
        modelAndView.addObject("userlogin2",userLogin2);
        modelAndView.addObject("userInf",userInf);

        if (session.getAttribute("userInf")!=null){
            UserInf userInf1= (UserInf) session.getAttribute("userInf");
            UserLogin user = (UserLogin) session.getAttribute("userLogin");
            modelAndView.addObject("user",user);
            modelAndView.addObject("userinf1",userInf1);
        }


        return modelAndView;
    }

    @PostMapping(value = "/houselist")
    public ModelAndView filterhouse(@RequestParam("area")List<String> arealist,@RequestParam("sort") String sort,@RequestParam("type")String type,@RequestParam("paymethod")String paymethod,HttpSession httpSession){
        ModelAndView modelAndView = new ModelAndView("house/houselist");
        List<Area> areas = areaService.findall();
        modelAndView.addObject("areas",areas);
        if (httpSession.getAttribute("userInf")!=null){
            UserLogin user = (UserLogin) httpSession.getAttribute("userLogin");
            modelAndView.addObject("user",user);
            modelAndView.addObject("userInf",(UserInf)httpSession.getAttribute("userInf"));
        }
        List<House> houseList = new ArrayList<>();
        for(String area:arealist){
            houseList.addAll(houseService.findByOrder(paymethod,type,area,sort));
        }
        modelAndView.addObject("list",houseList);
        return modelAndView;
    }
    @RequestMapping("/newhouse")
    public ModelAndView newHouse(HttpSession session){
        ModelAndView modelAndView=new ModelAndView();
        List<Area> areas = areaService.findall();
        modelAndView.addObject("areas",areas);
        if (session.getAttribute("userInf")!=null){
            UserLogin user = (UserLogin) session.getAttribute("userLogin");
            modelAndView.addObject("user",user);
            modelAndView.addObject("userInf",(UserInf)session.getAttribute("userInf"));
        }
        modelAndView.setViewName("house/newhouse");
        return modelAndView;
    }
    @RequestMapping("/donewhouse")
    @ResponseBody
    public JsonVo doNewHouse(@RequestParam("title")String title,@RequestParam("area")String area,@RequestParam("place")String place,@RequestParam("price")int price,@RequestParam("inf")String inf,@RequestParam("type")String type,@RequestParam("payType")String payType,@RequestParam("tag")String tag,HttpSession session){
        JsonVo jsonVo=new JsonVo();
        if (session.getAttribute("userInf")==null){ //用户未登录，提示登录
            jsonVo.setData("请登录");
            return jsonVo;
        }
        Integer id = new Random().nextInt(200); //解决插入之前无法获取房屋ID的问题
        UserLogin user = (UserLogin) session.getAttribute("userLogin"); //获取当前用户基本信息
        House house=new House();
        house.setId(id);
        house.setArea(place);
        house.setInf(inf);
        house.setTag(tag);
        house.setPayType(payType);
        house.setType(type);
        house.setCreateTime(new Date());
        house.setAreaUrl(area);
        house.setPrice(price);
        house.setUserId(user.getId());
        house.setStatus(0);
        house.setTitle(title);
        List<String> imageUrl= (List<String>) session.getAttribute("imageUrlList"); //获取session中保存的图片列表信息
        if (imageUrl!=null){
            for(int i=0;i<imageUrl.size();i++){
                if (i==0){
                    house.setMainImage(imageUrl.get(i));
                    houseService.insertHouse(house);
                }
                HouseImage houseImage=new HouseImage();
                houseImage.setHouseId(id);
                houseImage.setUrl(imageUrl.get(i));
                houseImageMapper.insert(houseImage);
                session.removeAttribute("imageUrlList"); //清理session中保存的图片列表信息
            }
        }



        return jsonVo;

    }
    @GetMapping("/houselist")
    public ModelAndView houseList(HttpSession httpSession){
        ModelAndView modelAndView=new ModelAndView();
        List<Area> areas = areaService.findall();
        modelAndView.addObject("areas",areas);
        List<House>houseList=houseService.findAllRelease();
        modelAndView.addObject("list",houseList);
        if (httpSession.getAttribute("userInf")!=null){
            UserLogin user = (UserLogin) httpSession.getAttribute("userLogin");
            modelAndView.addObject("user",user);
            modelAndView.addObject("userInf",(UserInf)httpSession.getAttribute("userInf"));
        }

        modelAndView.setViewName("house/houselist");
        return modelAndView;
    }
    @RequestMapping(value = "/views",method = RequestMethod.GET,params = {"id"})
    public ModelAndView getvr(@RequestParam("id")String id){
        ModelAndView modelAndView=new ModelAndView();
        House house = houseService.getById(Integer.parseInt(id));
        modelAndView.addObject("house",house);
        modelAndView.setViewName("house/360-photos");
        return modelAndView;
    }
    @RequestMapping(value = "/findbypay",method = RequestMethod.POST)
    @ResponseBody
    public JsonVo findByPay(@RequestParam("pay") String pay,@RequestParam("type") String type,@RequestParam("area") String area){
        JsonVo jsonVo=new JsonVo();
        List<House> houseList=houseService.findBy(pay,type,area);
        jsonVo.setData(houseList);
        return jsonVo;

    }

    @RequestMapping(value = "/user/news",method = RequestMethod.POST)
    @ResponseBody
    public JsonVo newsInto(@RequestParam("userId")int userId,@RequestParam("houseId")int houseId,@RequestParam("price")String price,@RequestParam("inf")String inf,@RequestParam("telphone")String telphone){
        JsonVo jsonVo=new JsonVo();
        newsService.insertOne(houseId,userId,telphone,price,inf);
        return jsonVo;
    }
    @RequestMapping(value = "/api/accusat",method = RequestMethod.POST)
    @ResponseBody
    public JsonVo newsInto(@RequestParam("houseId")int houseId,@RequestParam("inf")String inf,@RequestParam("result")String result,HttpSession session){
        UserInf userInf= (UserInf) session.getAttribute("userInf");
        if (userInf!=null){
            accusationService.insertOne(userInf.getId(),houseId,result,inf);
        }
        JsonVo jsonVo=new JsonVo();
        return jsonVo;
    }
    @PostMapping(value = "/house/look")
    @ResponseBody
    public JsonVo house(@RequestParam("id")int houseId,HttpSession session){
        UserInf userInf= (UserInf) session.getAttribute("userInf");
        JsonVo jsonVo=new JsonVo();

        if (userInf==null){
            jsonVo.setCode(500);
            return jsonVo;
        }
        collectService.insertOne(userInf.getUserLoginId(),houseId);
        jsonVo.setCode(200);
        return jsonVo;
    }
}
